<template>
  <div class="gs-wrap" v-loading="loading">
    <div class="gs-nav">我要卖货 > 热销商品 > 我要选货</div>
    <div class="gp-search">
      <div class="gp-search-top">
        <input v-model="searchText" type="text" placeholder="请输入搜索商品名称" @keyup.enter="searchShop"/>
        <div class="box-img" @click="searchShop">
          <!-- <img src="~assets/images/search-logo.png" /> -->
          <i class="icon iconfont iconicon-sousu-copy"></i>
        </div>
      </div>
      <div class="gp-search-bottom">
        <div class="gp-search-bottom-item gp-item-one">
          <p class="name category">商品分类：</p>
          <!-- <div class="list" v-if="typeList.length > 0">       -->
          <div class="list one">
            <p  :class="typeIndex === -1 ? 'item-active': ''"  @click="selectGoods(null,-1)">全部</p>     
             <p v-for="(item, index) in typeList" :key="index" :class="typeIndex === index ? 'item-active': ''"  @click="selectGoods(item.id,index)">{{item.name}}</p>     
          </div>
        </div>
        <div class="gp-search-bottom-item-b">
          <p class="sort">商品排序：</p>
          <div class="list">
            <p v-for="(item, index) in searchList" :key="index" :class="sortType == item.id ? 'item-active': ''" @click="selectSearchlist(item.id)">{{item.name}}</p>
            <div class="list-input">
              <p>价格</p>
              <div class="list-input-start">
                <p>¥</p>
                <input type="text" v-model="priceMin" @keyup.enter="searchShop">
              </div>
              &nbsp;&nbsp;-&nbsp;&nbsp;
              <div class="list-input-end">
                <p>¥</p>
                <input type="text" v-model="priceMax" @keyup.enter="searchShop">
              </div>
            </div>
          </div>
          <div class="priceQuery" @click="searchShop">确定</div>
        </div>
      </div>
    </div>
    <div class="gp-list">
      <template v-if="goodsList.length">
        <div class="gp-item" v-for="(item, index) in goodsList" :key="index">
          <!-- 传递item index 和商品id到hotGoodsItem中 -->
          <div class="gp-empty" v-if="item.isempty"></div>
          <hotGoodsItem :item="item" :index="index" v-else />
        </div>
      </template>
      <div class="no-wrap" v-else>商品加载中</div> 
    </div>
    <!-- <div class="pagination" v-show = "pageSize >= 8"> -->
    <div class="pagination"  v-show = "total > 0">
      <el-pagination
        background
        :current-page = "pageNum"
        @current-change="changeCurrentPage"
        :page-sizes="[200]"
        @size-change="changeCurrentPageSize"
        :total = "total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
      <div class="pageSearch" @click="pageSearch">确定</div>
   </div>
  </div>
</template>
<script>
import hotGoodsItem from '~/components/hotGoodsItem.vue' // 商品
export default {
  async asyncData(context) {
  },
  async fetch({store}) {
    await store.commit('setPromoteIndex', 0)
    await store.commit('setPromoteTwoIndex', 0)
  },
  layout: 'internalouts',
  components: {                 
    hotGoodsItem
  },
  data() {
    return {
      listShow: false,
      lineName: '完成订单量',
      typeList:[],
      typeIndex: -1,  //当前选中商品的类型
      searchList:[  //写死
        {
          name:'综合',
          id:0,
        },
        // {
        //   name:'佣金比例',
        //   id:1,
        // },
        // {
        //   name:'佣金',
        //   id:2,
        // },
        {
          name:'销量',
          id:3,
        },
      ], // 条件搜索
      sortType:0,//默认排序id
      goodsList: [], //商品列表
      goodsList1: [], //商品列表
      searchText:'', //搜索
      priceMax:'',  //最大价格区间
      priceMin:'', //最小价格区间
      cateId:'',  //分类ID
      pageNum:1, //第一页
			pageSize:200, //每页的条数
      total:0, //总条数
      loading:false, //页面加载效果
      screenWidth: 0,
    }
  },
  computed:{
    // user(){ //用户ID 
    //   return this.$store.state.user
    // }
  },
  methods: {
    changeCurrentPage(page){ //改变当前页数
      this.pageNum = page; //赋值给当前页
      this.hotsList(); //调用商品列表接口
      this.$scrollTo();
    },
    changeCurrentPageSize(pageSize){ //改变条数
      this.pageNum = 1; //重新请求第一页
      this.pageSize = pageSize;  //设置条数
      this.hotsList();//调用商品列表接口
      this.$scrollTo();
    },
    pageSearch(){ //点击确定按钮时候调用分页事件
      this.pageNum = 1; //重新请求第一页
      this.hotsList();//调用商品列表接口
      this.$scrollTo();
    },
    selectGoods(id,index) { // 选择商品类型
      if(index === -1){
        this.cateId = ''; //cateID为空
      }else{
        // this.cateId = this.typeList[index].id
        this.cateId = id;
      }
      this.typeIndex = index;
      this.pageNum = 1;  //重新请求第一页
      this.hotsList(); //我要选货商品列表
    },
    selectSearchlist(index) { // 选择排序方式
      this.sortType = index;
      this.pageNum = 1;  //重新请求第一页
      this.hotsList(); //我要选货商品列表
    },
    searchShop(){ //搜索商品
      this.pageNum =1; //重新请求第一页
      this.hotsList();//调用我要选货商品列表
    },
    enter(e) {
    },
    hotsList(){ //我要选货商品列表goodsList
      this.loading = true
      let params ={
        pageNum:this.pageNum, //页码数
        pageSize:this.pageSize, //每页的条数
        skuName:this.searchText, //商品名称
        sortType:this.sortType,  //排序类型
        priceMax:this.priceMax,  //最大价格区间
        priceMin:this.priceMin,  //最小价格区间
        cateId:this.cateId, //商品分类ID 要么不写传所有的，要么就是传递某一个
      }
      this.$axios.$post('hotsList',params).then(res => {
        this.loading = false;
        if(res.status === 200 ){
          this.goodsList = res.obj.list;
          this.goodsList1 = res.obj.list;
          this.total = res.obj.total; //拿到总的条数
          if(this.goodsList1.length > 0) {
            this.getColSize();
          }
        }else{
          this.$message.warning(res.msg);
        }
      })
      .catch(error => {
        this.$message.warning('请求失败');
        this.loading = false;
      })
    },
    goodsCateList(){ //商品信息之分类列表
      this.loading = true;
      this.$axios.$post('goodsCateList').then(res => {
        this.loading = false
        if(res.status === 200 ){
          this.typeList = res.obj;
        }else{
          this.$message.warning(res.msg);
         }
      })
      .catch(error => {
        this.$message.warning('请求失败');
        this.loading = false;
      })
    },
    // 补充商品空白
    getColSize() {
      let itemWidth = 288;
      let listWidth = this.screenWidth - 260;
      let colSize = Math.floor(listWidth / itemWidth);
      let space = this.goodsList1.length % colSize; //
      if(space <= 0){
        return;
      }
      let empty = colSize - space; //2-0表示
      this.goodsList = JSON.parse(JSON.stringify(this.goodsList1)); //深拷贝
      for (let index = 0; index < empty; index++) {
        const element = {
          isempty: true
        };
        this.goodsList.push(element);
      }
    },
  },  
  created(){
  },
  watch:{
    priceMin(newValue){
      this.priceMin = newValue.replace(/\D/g,'')
    },
    priceMax(newValue){
      this.priceMax = newValue.replace(/\D/g,'')
    },
  },
  mounted(){
    this.goodsCateList(); //商品信息之分类列表
    this.hotsList();  //我要选货商品列表
    this.screenWidth = document.body.clientWidth;
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth;
        if(this.goodsList1.length > 0) { //如果取余大于0,我们需要调用方法,添加空的DIV
          this.getColSize();
        }
      })();
    };
  },
}
</script>

<style scoped>
  .gs-nav {
    font-size: 14px;
    color: #999;
    height: 50px;
    line-height: 50px;
  }
  .gp-search {
    background: #fff;
    border-radius: 5px;
    padding: 20px;
  }
  .gp-search-top {
    display: flex;
    width: 264px;
    height: 30px;
    box-sizing:border-box;
    align-items: center;
    justify-content: space-between;
  }
  .gp-search-top .box-img {
    width: 40px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0096e6;
    cursor: pointer;
    border-top-right-radius:4px;
    border-bottom-right-radius:4px;
  }
  .gp-search-top .box-img:hover{
    background:#66B1FF;
  }
  .gp-search-top i{
    font-size:18px;
    color:#ffffff;
  }
  .gp-search-top input {
    font-size: 14px;
    color: #333;
    flex: 1;
    border: 1px solid #e1e1e1;
    border-right:0;
    box-sizing: border-box;
    text-indent: 12px;
    border-top-left-radius:4px;
    border-bottom-left-radius:4px;
  }
  .gp-search-top input:focus{
    border:1px solid #0096e6;
  }
  .gp-search-bottom {
    border: 1px solid #e1e1e1;
    margin-top: 20px;
    padding: 0 20px;
  }
  .gp-search-bottom-item {
    display: flex;
    color: #333;
    font-size: 14px;
  }
  .gp-search-bottom-item-b {
    display: flex;
    align-items:center;
    color: #333;
    font-size: 14px;
    height: 50px;
  }
   .gp-search-bottom-item-b .list {
    display: flex;
    align-items: center;
    flex-wrap:wrap;
  }
  .gp-item-one {
    border-bottom: 1px dashed #e8e8e8;
    margin-top:20px;
    padding-bottom:10px;
    color:#666666;
  }
  .gp-search-bottom-item .name {
    color: #666;
    font-size: 14px;
  }
  .gp-search-bottom-item .sort{
    color: #666;
    font-size: 14px
  }
  .gp-search-bottom-item .category{
    white-space:nowrap;
    margin-top:10px;
  }
  .gp-search-bottom-item .list {
    display: flex;
    align-items: center;
    flex-wrap:wrap;
  }
  .gp-search-bottom-item .list>p {
    margin-right: 34px;
    cursor: pointer;
    white-space:nowrap;
    padding:5px 14px 5px 15px;
    margin-top:5px;
    border-radius:4px;
  }
  .gp-search-bottom-item-b .list>p {
    margin-right: 34px;
    cursor: pointer;
    white-space:nowrap;
    padding:5px 14px 5px 15px;
  }
  .item-active {
    background: #f0f9fe;
    color: #0096e6;
    font-weight: bold;
  }
  .list-input {
    display: flex;
    align-items: center;
  }
  .list-input-start {
    display: flex;
    align-items: center;
    width: 54px;
    height: 24px;
    border: 1px solid #e1e1e1;
    margin-left: 8px;
  }
  .list-input-start p {
    color: #777;
    font-size: 12px;
    padding-left: 5px;
  }
  .list-input-start input {
    flex: 1;
    font-size: 12px;
    height: 24px;
    line-height: 24px;
  }
  .list-input-end {
    display: flex;
    align-items: center;
    width: 54px;
    height: 24px;
    border: 1px solid #e1e1e1;
  }
  .list-input-end p {
    color: #777;
    font-size: 12px;
    padding-left: 5px;
  }
  .list-input-end input {
    flex: 1;
    font-size: 12px;
    height: 24px;
    line-height: 24px;
  }
  .gp-list {
    margin-top: 20px;
    display: flex;
    /* min-width:980px; */
    width:100%;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .gp-list .gp-item {
    margin-bottom: 30px;
  }
  .gp-list .gp-empty {
    width: 288px;
    height: 428px;
    margin-bottom: 30px;
  }
  .gp-search-bottom-item-b .priceQuery{
    width:50px;
    height:26px;
    line-height:26px;
    text-align:center;
    border-radius:15px;
    background:#0096e6;
    color:#ffffff;
    font-size:14px;
    cursor:pointer;
    margin-left:10px;
  }
  .gp-search-bottom-item-b .priceQuery:hover{
    background:#66B1FF;
  }
  .pagination{
    display:flex;
    justify-content:flex-end;
    margin:15px 0;
  }
  .pagination .pageSearch{
    width:60px;
    height:28px;
    line-height:28px;
    text-align:center;
    border-radius:15px;
    background:#0096e6;
    color:#ffffff;
    font-size:14px;
    cursor:pointer;
    margin-left:10px;
    user-select:none;
  }
  .pagination .pageSearch:hover{
    background:#66B1FF;
  }
  /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active{
    background-color:#0096e6;
  }
  /* 暂无数据的css样式 */
  .no-wrap{
    width:100%;
    height:40px;
    line-height:40px;
    text-align:center;
    background:#ffffff;
    font-weight:bold;
    letter-spacing:2px;
    color:#666666;
    font-size:18px;
    border-radius:4px;
    box-shadow:0 0 6px #c2c2c2;
    margin:0 0 20px 0;
  } 
</style>
 